package com.colin.app.activity.view.popupwindow;

import android.os.Build;
import android.view.Gravity;
import android.view.View;

import com.colin.app.R;
import com.colin.app.activity.view.ViewContract;
import com.colin.app.activity.view.ViewPresenterImpl;
import com.colin.app.app.AppActivity;
import com.colin.app.bean.DemoBean;
import com.colin.app.popupwindow.ListViewPopupWindow;
import com.colin.library.help.LogHelp;
import com.colin.library.other.Constants;

public class PopupWindowActivity extends AppActivity implements ViewContract.ViewView {
    private ViewContract.ViewPresenter mViewPresenter = null;
    private ListViewPopupWindow listViewPopupwindow = null;
    private ListViewPopupWindow listViewPopupwindowLeft = null;
    private ListViewPopupWindow listViewPopupwindowRight = null;
    private ListViewPopupWindow listViewPopupwindowTop = null;
    private ListViewPopupWindow listViewPopupwindowBottom = null;

    @Override
    protected void onDestroy() {
        listViewPopupwindow = null;
        listViewPopupwindowLeft = null;
        listViewPopupwindowRight = null;
        listViewPopupwindowTop = null;
        listViewPopupwindowBottom = null;
        if (null != mViewPresenter) {
            mViewPresenter.destroy();
            mViewPresenter = null;
        }
        super.onDestroy();
    }

    @Override
    public void initPresenter() {
        DemoBean demoBean = getIntent().getExtras().getParcelable("clazz");
        mViewPresenter = new ViewPresenterImpl(this, this, demoBean);
    }


    @Override
    public int initViewLayoutID() {
        return R.layout.activity_popup_window;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {
        mViewPresenter.setData();
    }


    public void showPopupWindowList(View view) {
        showListPopupWindow(view);
    }

    public void showPopupWindowListLeft(View view) {
        showListPopupWindowLeft(view);
    }

    public void showPopupWindowListRight(View view) {
        showListPopupWindowRight(view);
    }

    public void showPopupWindowListTop(View view) {
        showListPopupWindowTop(view);
    }

    public void showPopupWindowListBottom(View view) {
        showListPopupWindowBottom(view);
    }

    private void showListPopupWindowLeft(View view) {
        if (listViewPopupwindowLeft == null) {
            listViewPopupwindowLeft = new ListViewPopupWindow(this, Constants.FRAGMENT_RECYCLER_VIEW_TAG, "设置");
        }

        // 显示
        if (Build.VERSION.SDK_INT >= 19) {
            listViewPopupwindowLeft.showAsDropDown(view, view.getWidth() / 2 - listViewPopupwindowLeft.getWidth() / 2, 0, Gravity.LEFT);
            LogHelp.d(Build.VERSION.SDK_INT);
        }
        //监听
        listViewPopupwindowLeft.setOnListViewInPopupwindowListener(new ListViewPopupWindow.OnListViewInPopupWindowListener() {
            @Override
            public void onItemCheck(int position, String string) {
                listViewPopupwindowLeft.dismiss();
                listViewPopupwindowLeft = null;
                toastShow(string);
            }
        });
    }

    private void showListPopupWindowRight(View view) {
        if (listViewPopupwindowRight == null) {
            listViewPopupwindowRight = new ListViewPopupWindow(this, Constants.FRAGMENT_RECYCLER_VIEW_TAG, "设置");
        }

        // 显示
        if (Build.VERSION.SDK_INT >= 19) {
            listViewPopupwindowRight.showAsDropDown(view, view.getWidth() / 2 - listViewPopupwindowRight.getWidth() / 2, 0, Gravity.RIGHT);
            LogHelp.d(Build.VERSION.SDK_INT);
        }
        //监听
        listViewPopupwindowRight.setOnListViewInPopupwindowListener(new ListViewPopupWindow.OnListViewInPopupWindowListener() {
            @Override
            public void onItemCheck(int position, String string) {
                listViewPopupwindowRight.dismiss();
                listViewPopupwindowRight = null;
                toastShow(string);
            }
        });
    }

    private void showListPopupWindowTop(View view) {
        if (listViewPopupwindowTop == null) {
            listViewPopupwindowTop = new ListViewPopupWindow(this, Constants.FRAGMENT_RECYCLER_VIEW_TAG, "设置");
        }

        // 显示
        if (Build.VERSION.SDK_INT >= 19) {
            listViewPopupwindowTop.showAsDropDown(view, view.getWidth() / 2 - listViewPopupwindowTop.getWidth() / 2, 0, Gravity.TOP);
            LogHelp.d(Build.VERSION.SDK_INT);
        }

        listViewPopupwindowTop.update();
        //监听
        listViewPopupwindowTop.setOnListViewInPopupwindowListener(new ListViewPopupWindow.OnListViewInPopupWindowListener() {
            @Override
            public void onItemCheck(int position, String string) {
                listViewPopupwindowTop.dismiss();
                listViewPopupwindowTop = null;
                toastShow(string);
            }
        });
    }

    private void showListPopupWindowBottom(View view) {
        if (listViewPopupwindowBottom == null) {
            listViewPopupwindowBottom = new ListViewPopupWindow(this, Constants.FRAGMENT_RECYCLER_VIEW_TAG, "设置");
        }

        // 显示
        if (Build.VERSION.SDK_INT >= 19) {
            listViewPopupwindowBottom.showAsDropDown(view, view.getWidth() / 2 - listViewPopupwindowBottom.getWidth() / 2, 0, Gravity.BOTTOM);
            LogHelp.d(Build.VERSION.SDK_INT);
        }
        //监听
        listViewPopupwindowBottom.setOnListViewInPopupwindowListener(new ListViewPopupWindow.OnListViewInPopupWindowListener() {
            @Override
            public void onItemCheck(int position, String string) {
                listViewPopupwindowBottom.dismiss();
                listViewPopupwindowBottom = null;
                toastShow(string);
            }
        });
    }

    private void showListPopupWindow(View view) {
        if (listViewPopupwindow == null) {
            listViewPopupwindow = new ListViewPopupWindow(this, Constants.FRAGMENT_RECYCLER_VIEW_TAG, "设置");
        }
        // 显示
        listViewPopupwindow.showAsDropDown(view, 0, 0);
        //监听
        listViewPopupwindow.setOnListViewInPopupwindowListener(new ListViewPopupWindow.OnListViewInPopupWindowListener() {
            @Override
            public void onItemCheck(int position, String string) {
                listViewPopupwindow.dismiss();
                listViewPopupwindow = null;
                toastShow(string);
            }
        });
    }


}
